package com.lst.projectlib.component.datetimepicker;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import com.lst.projectlib.R;

public class SingleTimePickerActivity extends Activity implements
		OnClickListener, OnTimeChangedListener {

	public static final String INTENT_BUNDLE_KEY_INITIAL_HOUR = "initial_hour";
	public static final String INTENT_BUNDLE_KEY_INITIAL_MINUTE = "initial_minute";
	public static final String INTENT_BUNDLE_KEY_INITIAL_TITLE = "initial_title";
	public static final String INTENT_BUNDLE_KEY_SELECT_SINGLE_TIME = "select_single_time";
	
	private TimePicker mTimePicker;

	int mInitialHourOfDay;
	int mInitialMinute;
	boolean mIs24HourView = true;
	int request_code = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_single_time_picker);

		TextView tv_ok = (TextView) this.findViewById(R.id.tv_ok);
		TextView tv_cancel = (TextView) this.findViewById(R.id.tv_cancel);
		tv_ok.setOnClickListener(this);
		tv_cancel.setOnClickListener(this);

		mTimePicker = (TimePicker) this.findViewById(R.id.time_picker);

		// initialize state
		mTimePicker.setIs24HourView(mIs24HourView);
		mTimePicker.setOnTimeChangedListener(this);
		
		initData();
	}

	private void initData() {
		Intent intent = getIntent();

		if (intent != null) {
			mInitialHourOfDay = intent.getIntExtra(INTENT_BUNDLE_KEY_INITIAL_HOUR, 0);
			mInitialMinute = intent.getIntExtra(INTENT_BUNDLE_KEY_INITIAL_MINUTE, 0);
			mTimePicker.setCurrentHour(mInitialHourOfDay);
			mTimePicker.setCurrentMinute(mInitialMinute);
			request_code = intent.getIntExtra("request_code", 0);
//			txt_title.setText(intent.getStringExtra(INTENT_BUNDLE_KEY_INITIAL_TITLE));
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.tv_cancel) {
			finish();
		} else if (v.getId() == R.id.tv_ok) {
			tryNotifyTimeSet();
			finish();
		}
	}

	public void updateTime(int hourOfDay, int minutOfHour) {
		mTimePicker.setCurrentHour(hourOfDay);
		mTimePicker.setCurrentMinute(minutOfHour);
	}

	private void tryNotifyTimeSet() {
		if (mTimePicker != null) {
			mTimePicker.clearFocus();
			
			Intent intent = new Intent();
			intent.putExtra("request_code", request_code);
//			intent.putExtra("select_hour", mTimePicker.getCurrentHour());
//			intent.putExtra("select_minute", mTimePicker.getCurrentMinute());
			
			String leftHour = mTimePicker.getCurrentHour() < 10 ? ("0" + mTimePicker.getCurrentHour()) : String.valueOf(mTimePicker.getCurrentHour());
			String leftMinute = mTimePicker.getCurrentMinute() < 10 ? ("0" + mTimePicker.getCurrentMinute()) : String.valueOf(mTimePicker.getCurrentMinute());
			
			intent.putExtra(INTENT_BUNDLE_KEY_SELECT_SINGLE_TIME, leftHour + ":" + leftMinute);
			this.setResult(RESULT_OK, intent);
		}
	}

	@Override
	public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

	}
}
